# -*- coding: utf-8 -*-
import scrapy


class GiteeloginSpider(scrapy.Spider):
    name = 'giteelogin'
    allowed_domains = ['gitee.com']
    start_urls = ['http://gitee.com/login']

    def parse(self, response):
        token = response.xpath('//*[@id="new_user"]/div[1]/input[2]/@value').get()

        post_data = {
            "encrypt_key": "password",
            "utf8": "✓",
            "authenticity_token": token,
            "redirect_to_url": "",
            "user[login]": "admin@dbsgw.cn",
            "encrypt_data[user[password]]": "NoR3LHIHbt1SE4MDtFjw/6eJZOONdfdCNIQC19oxJEtuar9oyn5vMN0f/usmJxmx35DFdAj3ZI2ZfA/13CXBtLWwchk6qtu94t5zrX8yfBoYh71kpcNGUyD/hTBz2yCbYwpzLTCccM8+NtigMgrYgU0kJHLQRKJsrHWi2G6v8DQ=",
            "user[remember_me]": "0"
        }
        print(post_data)

        yield scrapy.FormRequest(
            url="https://gitee.com/check_user_login",
            callback=self.arter_login,
            formdata=post_data
        )

    def arter_login(self, response):
        yield scrapy.Request('https://gitee.com/projects/new', callback=self.gitee)

    def gitee(self, response):
        print(response.body)
        print(response.xpath('//*[@id="new-project-form"]/h2/text()').get())
